<?php

/**
 * This file is part of the Pagizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Pagizer Core Team <team@pagizer.org>
 * @package     Pagizer
 * @subpackage  backoffice
 */

/**
 * Ajax controller, used by loadContent() in jQueryBackend.js
 * It get the "ajaxed" url (stuff after # in the url) and get the adapted display
 */

class m_backoffice_actions_AjaxControllerAction extends f_core_Action
{
	public function execute()
	{
        @set_error_handler(array('m_backoffice_extends_Error','errorHandler'));
        @set_exception_handler(array('m_backoffice_extends_Exception','getError'));	
	
		// Get requested url (href)
		$href = $this->getRequest()->getParameter('href');

		// Set to the routing class (see webapp/config/routes.xml to set routes)
		$routing = f_core_Routing::getInstance()->setRequestURL(substr($href,1));

		// Get parameters
		$module = $this->getRequest()->getParameter("module");
		$action = $this->getRequest()->getParameter("action");
		$docId = $this->getRequest()->getParameter("docId");
		$docLang = $this->getRequest()->getParameter("docLang", $this->getContextLang());
		$tabName = $this->getRequest()->getParameter("tabAction");
		$tabbed = $this->getRequest()->getParameter("tabbed");
        $isSystem = !is_null($this->getRequest()->getParameter("panel"));

        if(isset($docId) && isset($docLang))
        {
            $document = $this->getDocumentProvider()->getByUniqueId($docId, $docLang);

            if(is_object($document))
            {
                // Set a new context
                f_core_Context::getInstance()->setContext('currentDocument', $document);
            }
        }

        /*
         * @TOTO: find another method to check if the user is working on a document (same in ContextMenu and SliderMenu)
        $doc_checker = f_document_Provider::getInstance();
        if(!$doc_checker->checkDocument($docId, $docLang))
        {
            $request = $this->getRequest()->setParameter("docId", null);
            $docId = null;
        }
        */
				
		// Refresh the block context
		$blockContext = f_context_Block::getInstance();
		$blockContext->removeParameters();

		// If we get at last action and module from parsed url
		if(!is_null($module) && !is_null($action) && $action != "AjaxController")
		{
            $modules = array('website', 'medias', 'users', 'system');

            $moduleToSave = $module;

            if(!in_array($module, $modules))
            {
                $moduleToSave = 'modules';
            }
            
            if($isSystem)
            {
                $moduleToSave = 'system';
            }

            $session = $this->getSession();
            
            $session->setValue('module'.$moduleToSave.'url', $href);
            $session->setValue('lastUrl', $href);

            $urls['lastUrl'] = $session->getValue('lastUrl', f_core_Routing::getUrl('websiteListWoId'));
            $urls['modulewebsiteurl'] = $session->getValue('modulewebsiteurl', f_core_Routing::getUrl('websiteListWoId'));
            $urls['modulemediasurl'] = $session->getValue('modulemediasurl', f_core_Routing::getUrl('mediasRoot'));
            $urls['moduleusersurl'] = $session->getValue('moduleusersurl', f_core_Routing::getUrl('usersList'));
            $urls['modulemodulesurl'] = $session->getValue('modulemodulesurl', f_core_Routing::getUrl('modulesRoot'));
            $urls['modulesystemurl'] = $session->getValue('modulesystemurl', f_core_Routing::getUrl('systemRoot'));

            f_core_Cookie::getInstance()->setParameters($this->getUserContext()->getUser()->getLogin(), time()+2592000)->setCookie($urls);
            
			// Define the block context
			$blockContext->setParameter("documentId", $docId);
			$blockContext->setParameter("tabName", $tabName);

			$controller = f_core_Controller::getInstance();

			if($tabbed == "true" && !is_null($tabName))
			{
				return $controller->setModule($module)->setAction("block_".$tabName)->dispatch();
			}
			
			// Get the content
			return $controller->setModule($module)->setAction("block_".$action)->dispatch();
		}

        $session = $this->getSession();
        $urls['lastUrl'] = null;
        $urls['modulewebsiteurl'] = $session->getValue('modulewebsiteurl', f_core_Routing::getUrl('websiteListWoId'));
        $urls['modulemediasurl'] = $session->getValue('modulemediasurl', f_core_Routing::getUrl('mediasRoot'));
        $urls['moduleusersurl'] = $session->getValue('moduleusersurl', f_core_Routing::getUrl('usersList'));
        $urls['modulemodulesurl'] = $session->getValue('modulemodulesurl', f_core_Routing::getUrl('modulesRoot'));
        $urls['modulesystemurl'] = $session->getValue('modulesystemurl', f_core_Routing::getUrl('systemRoot'));
        f_core_Cookie::getInstance()->setParameters($this->getUserContext()->getUser()->getLogin(), time()+2592000)->setCookie($urls);
		
		return $this->launchAction("backoffice", "block_DashBoard");
	}	
	
	public function validate()
	{
		return true;
	}
}